#include<stdio.h>
#define N 5
#define M 3

/*
int input_arr[N] = {1, 2, 3, 4, 5};
int path[M];
*/

void copy(int *src, int *des, int len)
{
  int i;
  for (i = 0; i != len; ++i)
  {
    *des++ = *src++;
  }
}

/*
void back_tracking(int res[][M], int *pos_res, int *pos_path, int m, int start_index)
{
  if (m == 0)
  {
    copy(path, res[*pos_res], M);
    ++*pos_res;
  }
  else
  {
    int i;
    for (i = start_index; i < N; ++i)
    {
      path[*pos_path] = input_arr[i];
      ++*pos_path;
      back_tracking(res, pos_res, pos_path, m - 1, i + 1);
      --*pos_path;
    }
  }
}


void back_tracking(int res[][M], int *pos_res, int pos_path, int m, int start_index)
{
  if (m == 0)
  {
    copy(path, res[*pos_res], M);
    ++*pos_res;
  }
  else
  {
    int i;
    for (i = start_index; i < N; ++i)
    {
      path[pos_path] = input_arr[i];
      ++pos_path;
      back_tracking(res, pos_res, pos_path, m - 1, i + 1);
      --pos_path;
    }
  }
}
*/
void back_tracking(int path[], int input_arr[], int res[][M], int *pos_res, int pos_path, int m, int start_index)
{
  if (m == 0)
  {
    copy(path, res[*pos_res], M);
    ++*pos_res;
  }
  else
  {
    int i;
    for (i = start_index; i < N; ++i)
    {
      path[pos_path] = input_arr[i];
      ++pos_path;
      back_tracking(path, input_arr, res, pos_res, pos_path, m - 1, i + 1);
      --pos_path;
    }
  }
}

int main()
{
  int input_arr[N] = {1, 2, 3, 4, 5};
  int path[M];
  int result[10][M];
  int a = 0, b = 0, *pa = &a, *pb = &b;
  back_tracking(path, input_arr, result, pa, 0, M, 0);

  int i, cnt = 10, j;
  
  for (i = 0; i < cnt; ++i)
  {
    for (j = 0; j < M; ++j)
    {
      printf("%d ", result[i][j]);
    }
    printf("\n");
  }

  return 0;
}
